<?php

/**
 * SuperClass for field's type classes
 */
class fields_common extends filters
{

   protected $system_fields = array('alias', 'i_onpage', 'active', 'sdate', 'edate');
   protected $system = FALSE;
   protected $value  = NULL;
   public    $fieldset = FALSE;

   protected $save_columns = array(
         'id',
         'node',
         'active',
         'tab',
         'position',
         'alias',
         'type',
         'lang_phrase',
         'belong',
         'required',
         'translate',
         'default'
      );

   protected $escaped_columns = array(
         'system_fields',
         'system',
         'fieldset',
         'title',
         'value'
      );


   /**
    * Prepare Init data for Field
    *
    * @param array $data
    * @return array
    */
   public function __construct($data)
   {
      parent::__construct();

      if (is_array($data))
      {
         foreach ($data AS $name => $value)
         {
            $this->$name = $value;
         }

         $this->system = property_exists($this, 'alias') && in_array($this->alias, $this->system_fields) ? TRUE : FALSE;
         (bool) $this->active;

         $this->title = gettext::translate('fields', $this->lang_phrase);
      }
   }


   public function value($lang = NULL)
   {
      if(!property_exists($this, 'value') || $this->value === null)
      {
         return $this->default;
      }

      $lang = !empty($lang) ? request::getLang('data', $lang) : request::getLang('current');
      $lang = $lang['id'];
      if($this->translate)
      {
         return $this->value[$lang];
      } else
      {
         return $this->value;
      }
   }


   public function saveValue($lang = NULL)
   {
      if(property_exists($this, 'value'))
      {
         if($this->translate && !empty($lang))
         {
            if(isset($this->value[$lang]))
            {
               return $this->value[$lang];
            }
         } elseif(empty($lang))
         {
            return $this->value;
         }
      } elseif(property_exists($this, 'default'))
      {
         return $this->default;
      }

      return '';
   }


   public function isSystem()
   {
      return (bool)$this->system;
   }

   public function turnOn()
   {
      if(empty($this->id)) return FALSE;
      // TODO: change activity status, and then call ->save method to save properties
      // TODO: develop ->save method, in the end of its execution call rebuild field's tree method
      $this->active = 1;
      $DB = database::getInstance();
      $DB->sql = 'UPDATE `meccano_fields` SET `active` = "1" WHERE `id` = '. $this->id;
      return $DB->DB_Query();
   }

   public function turnOff()
   {
      if(empty($this->id)) return FALSE;

      $this->active = 0;
      $DB = database::getInstance();
      $DB->sql = 'UPDATE `meccano_fields` SET `active` = "0" WHERE `id` = '. $this->id;
      return $DB->DB_Query();
   }

   /**
    * Setter
    *
    * @param string $name
    * @param string $value
    */
   public function __set($name, $value)
   {
      $this->$name = $value;
   }

   public function sweep(){}

   public function save(){}
}